﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Leo.Blog.BLL.SysModule;
using Leo.Blog.Common;
using Leo.Blog.ViewModel.SysModule;
using Leo.Blog.Common.Encrypt;
using Leo.Blog.Common.Redis;

namespace Leo.Blog.Controllers
{
    public class LoginController : Controller
    {
        private Sys_Account_B _accountB= new Sys_Account_B();
        // GET: Login
        public ActionResult Index()
        {
            string guid = Guid.NewGuid().ToString("n");
            RedisHelper.SetStringKey(guid, guid);
            return View();
        }
        public ActionResult VerifyImage()
        {
            string code = (new VerifyImage()).GenerateCheckCode();
            Session["ValidateCode"] = code;
            byte[] bytes = new VerifyImage().CreateCheckCodeImage(code);
            return File(bytes, "image/jpeg");
        }
        [HttpPost]
        public ActionResult Index(LoginVM model)
        {
            string code = Session["ValidateCode"].ToString();
            if (code.ToLower().Equals(model.ValidCode.ToLower()))
            {
                string password = EncryptTools.GetMD5(model.Password);
                UserAccountVM accountVM = _accountB.Login(model.UserId, password);
                if (accountVM == null)
                {
                    model.Msg = "账号或密码不正确！";
                    return View(model);
                }
                Session["UserAccount"] = accountVM;
                return RedirectToAction("Index", "Home");
            }
            else {
                model.Msg = "验证码不正确！";
                return View(model);
            }
        }
        public ActionResult LoginAccess()
        {
                UserAccountVM accountVM = _accountB.Login();
                Session["UserAccount"] = accountVM;
                return RedirectToAction("Index", "Home");
        }
        public ActionResult Logout()
        {
            HttpContext.Session.Clear();
            return RedirectToAction("Index", "Login");
        }
    }
}